﻿using AzukiMap.Map;
using AzukiMap.MapSource;
using Geometries = GisSharpBlog.NetTopologySuite.Geometries;
using GeoAPI.Geometries;

namespace AzukiMap.Layer
{
    public class OpenStreetMapLayer : MercatorQuadLayer
    {
        OpenStreetMapSource _osmSource;

        public OpenStreetMapLayer()
            : this(new OpenStreetMapSource(OpenStreetMapTileType.OSMARENDER), "__osmLayer__" )
        {
        }

        public OpenStreetMapLayer(OpenStreetMapSource tileSource, string name)
            : base(tileSource, name)
        {
            _osmSource = tileSource;
        }

        protected override string CreateTileReqString(double tileScale, int pixX, int pixY)
        {
            int level = GetScaleLevel(tileScale);
            int tileX = pixX / _osmSource.TileWidth;
            int tileY = pixY / _osmSource.TileHeight;

            return level.ToString() + "/" + tileX.ToString() + "/" + tileY.ToString() + ".png";
        }

        protected override IPoint GetGeoPointFromTileCoord(int tx, int ty, double tileScale)
        {
            double lon = 0;
            double lat = 0;
            int level = GetScaleLevel(tileScale); 
            VirtualEarthTileSystem.PixelXYToLatLong(tx, ty, level, out lat, out lon);

            return new Geometries.Point(lon, lat){SRID = 4326};
        }

    }
}
